//app.js
const quote = require('utils/quote.config.js') //统一引入文件
const Recorder = quote.Recorder //引入外部录音接口
const api = quote.api;
// const aldstat = quote.aldstat;
const mta = quote.mta;
const tools = quote.tools;
const DataStorage = quote.DataStorage;
const WXlogin = quote.WXlogin;
const recorderTools = quote.recorderTools;

App({

    onLaunch: function(options) {
        var self = this
            //获取本地缓存的openid 
        console.log('onLaunch,第一次加载小程序获取到的数据', options);

        if (options.scene) {
            console.log('打开小程序的场景值', options.scene);
            if (options.scene == 1044) {
                console.log('群票据', options.shareTicket)
            }
        }

        //微信登录获取用户openId
        new WXlogin(this, api);
        console.log('this', this)

        setTimeout(() => {
            //初始化腾讯分析
            mta.App.init({
                "appID": "500589838",
                "eventID": "500589890", // 高级功能-自定义事件统计ID，配置开通后在初始化处填写
                "lauchOpts": options, //渠道分析,需在onLaunch方法传入options,如onLaunch:function(options){...}
                "statPullDownFresh": true, // 使用分析-下拉刷新次数/人数，必须先开通自定义事件，并配置了合法的eventID
                "statShareApp": true, // 使用分析-分享次数/人数，必须先开通自定义事件，并配置了合法的eventID
                "statReachBottom": true // 使用分析-页面触底次数/人数，必须先开通自定义事件，并配置了合法的eventID
            });

            //获取用户的设备信息
            wx.getSystemInfo({
                success: function(res) {
                    self.globalData.deviceInfo = {
                        model: res.model, //模型 iPhone 5s (GSM+CDMA)<iPhone6,2>
                        pixelRatio: res.pixelRatio, //像数比例
                        platform: res.platform, //系统平台
                        system: res.system, //系统版本 "iOS 8.4.1"
                        windowHeight: res.windowHeight, //窗口高度
                        windowWidth: res.windowWidth, //窗口宽度
                    }
                    var width = parseInt(res.windowWidth)
                    var height = parseInt(res.windowHeight)
                    var scale = parseFloat(res.windowWidth) / 375.0
                    var scaleH = parseFloat(res.windowHeight) / 667.0
                    self.globalData.windowWidth = width;
                    self.globalData.windowHeight = height;
                    self.globalData.scale = scale;
                    console.log('用户设备信息', res, self.globalData.deviceInfo)
                }
            })
        }, 5000);

        // 获取用户是否登录
        wx.getStorage({
            key: 'loginStatus',
            success: function(res) {
                console.log(res)
                self.globalData.loginStatus = res.data
            },
            fail: function(res) {
                self.globalData.loginStatus = false
            }
        })

        //小程序如果是当天第一次打开往缓存里面写入一个默认练习次数
        var date = new Date().getDate();
        var firstOpen = wx.getStorageSync('firstOpen');
        console.log('firstOpen', firstOpen);
        if (!firstOpen) { //如果不存在firstOpen，则写入一个数据
            wx.setStorageSync('firstOpen', { 'date': date, 'DefaultTimes': 1, 'redPaperIsOpened': false });
            self.globalData.DefaultTimes = 1;
        } else {
            if (firstOpen.date != date) { //如果有firstOpen，看日期是不是当天，如果不是写入一个数据
                wx.setStorageSync('firstOpen', { 'date': date, 'DefaultTimes': 1, 'redPaperIsOpened': false });
                return;
            }
            //如果有firstOpen，而且是当天，则读取原来的数据
            self.globalData.DefaultTimes = firstOpen.DefaultTimes;
            self.globalData.redPaperIsOpened = firstOpen.redPaperIsOpened
        }


    },
    onShow: function(options) {
        var that = this;

        console.log('onShow,进入小程序获取到的数据', options);

        //获取缓存中的UID
        const UID = wx.getStorageSync('UID');
        if (UID) {
            that.globalData.UID = UID;
            console.log('缓存中获取到的UID', UID);
        }

        //获取缓存中的openId
        const openId = wx.getStorageSync('openid');
        if (openId) {
            that.globalData.openId = openId;
            console.log('缓存中获取到的openId', openId);
        }


        //如果为转发获取转发信息
        // wx.getShareInfo({
        //     success: function(res) {
        //         console.log('获取转发信息成功', res)
        //     },
        //     fail: function(err) {
        //         console.log('获取转发信息失败', err)
        //     },
        //     complete: function(msg) {
        //         console.log('获取转发信息接口完成', msg)
        //     }
        // });

    },
    onHide: function() {
        var self = this;
        console.log('onHide,小程序隐藏了');

        //发送formId到后台
        var openId = self.globalData.openId || wx.getStorageSync('openid');
        var formIds = wx.getStorageSync('formId');

        if (!openId || !formIds) {
            return
        }

        tools.SendFormId(openId, formIds)

    },
    onError: function(msg) {
        console.log(msg)
    },

    globalData: {
        ADisPop: false, //广告是否已经弹出
        AlreadyPracticed: {}, //已经练习过的视频列表
        DefaultTimes: 0, //每日挑战的默认练习次数
        AccountsTimes: 0, //账户中获取的练习次数
        SDKVersion: null,
        userInfo: null,
        certificationOk: 0, //是否认证
        appId: 'wx820493357a58f254', //微信小程序的appId
        appSecret: '5a6ec00b69f935b4453934a73e5de1c2', //微信小程序的密钥
        appkey: '049BD15C6FC04BD80808A601DC46E50515CBEEA33FB29AB4',
        appSource: "MiniappTTKYX_578009",
        wxCode: null,
        session_key: null, //会话密钥
        UID: 420683, //微信中用户唯一标识符
        openId: null, //小程序中用户唯一标志符o_f4J0RLl9LpHOc1Ra_Q3gvAkoLo
        loginStatus: false, //登录状态
        videoUrl: "https://video.acadsoc.com.cn",
        apiUrl: "https://ies.acadsoc.com.cn",
        baseAshx: "/ECI/WeChatSmallProgram/WeChat_Base.ashx", //域名虚拟路径
        mediaUrl: "https://admin.acadsoc.com.cn", //图片及录音地址
        //  videoUrl: "http://192.168.74.98:8085", //测试视频地址"http://192.168.74.98:8085"
        // apiUrl: "http://192.168.74.224:8085", //测试api地址"http://192.168.74.189:8000"
        timer: 30000, //定时器设置时间
        access_token: null, //访问令牌
        recorder: new Recorder('1510645856000042', '412ab8c468fcd728c584990850d3a77d'), //创建一个 Recorder 对象
        innerAudioContext: wx.createInnerAudioContext(), //创建一个音频播放器
        videoCacheObj: {},
        redPaperIsOpened: null,
    },
})